package com.autosalon.ui;

import com.autosalon.model.Zakaznik;
import com.autosalon.ui.auth.Authentication;
import com.autosalon.ui.state.MainGuiWindowStateHolder;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.VerticalSplitPanel;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.BaseTheme;
import com.vaadin.ui.themes.ChameleonTheme;

/**
 * @author tomas
 * 
 */
public class MainGuiWindow extends Window {

	private final Label title;
	private final Label content;
	private final Label loginHeader;
	private final Label registerHeader;
	private final Button menuItem1;
	private final Button menuItem4;
	private final Button menuItem2;
	private final Button menuItem3;
	private final Button menuItem6;
	private final Button menuItem7;
	private final Button menuItem8;
	private final Button menuItem9;
	private final Button confirmLoginButton;
	private final Button regiterButton;
	private final HorizontalLayout menuLayout;
	private final VerticalLayout mainLayout;
	private final VerticalLayout titleLayout;
	private final VerticalLayout loginLayout;
	private final TextField username;
	private final PasswordField password;
	private final OptionGroup loggedOptionGroup;
	private final VerticalSplitPanel verticalSplitPanel;
	private final HorizontalSplitPanel horizontalSplitPanel;
	private ContentSwitcher switcher = new ContentSwitcher(this,
			ContentSwitcher.HOME_PANEL);
	public Zakaznik zakaznik;

	public MainGuiWindow() {

		setTheme("chameleon-blue");

		title = new Label("Autosalon");
		title.addStyleName(ChameleonTheme.LABEL_H1);
		title.addStyleName(ChameleonTheme.LABEL_COLOR);

		content = new Label("<h1>Vítejte v autosalonu</h1>"
				+ "<h2>Informace o autosalonu:</h2>" + "<ul>"
				+ "<li>Adresa: Ostrava, Studentské koleje</li>"
				+ "<li>Kontakt: 608034123</li>" + "</ul>"
				+ "<h3>Registrací získáte:</h2>" + "<ul>"
				+ "<li>Možnost objednání nového automobilu</li>"
				+ "<li>Přehled o svých objednávkách</li>" + "</ul>");
		getContentLabel().setContentMode(Label.CONTENT_XHTML);

		loginHeader = new Label("Přihlásit se");
		loginHeader.addStyleName(ChameleonTheme.LABEL_H2);
		loginHeader.addStyleName(ChameleonTheme.LABEL_COLOR);

		registerHeader = new Label("Nejste dosud zaregistrováni?");
		regiterButton = new Button("Tak se registrujte");
		regiterButton.setStyleName(BaseTheme.BUTTON_LINK);

		username = new TextField("Email:");

		password = new PasswordField("Heslo:");

		menuItem1 = new Button("Domů");
		menuItem4 = new Button("Registrace");
		menuItem2 = new Button("Nabídka automobilů");
		menuItem3 = new Button("Nabídka doplňků");
		menuItem7 = new Button("Správa objednávek");
		menuItem6 = new Button("Správa pojištení");
		menuItem8 = new Button("Správa bank");
		menuItem9 = new Button("Správa půjček");
		confirmLoginButton = new Button("Přihlásit");
		confirmLoginButton.addListener(new Authentication(this));
		menuItem1.addStyleName(ChameleonTheme.BUTTON_DEFAULT);
		menuItem2.addStyleName(ChameleonTheme.BUTTON_DEFAULT);
		menuItem4.addStyleName(ChameleonTheme.BUTTON_DEFAULT);
		menuItem3.addStyleName(ChameleonTheme.BUTTON_DEFAULT);
		menuItem7.addStyleName(ChameleonTheme.BUTTON_DEFAULT);
		menuItem6.addStyleName(ChameleonTheme.BUTTON_DEFAULT);
		menuItem8.addStyleName(ChameleonTheme.BUTTON_DEFAULT);
		menuItem9.addStyleName(ChameleonTheme.BUTTON_DEFAULT);
		confirmLoginButton.addStyleName(ChameleonTheme.BUTTON_WIDE);
		registerHeader.addStyleName(ChameleonTheme.BUTTON_WIDE);
		regiterButton.addStyleName(ChameleonTheme.BUTTON_WIDE);

		verticalSplitPanel = new VerticalSplitPanel();
		verticalSplitPanel.setSplitPosition(100, Sizeable.UNITS_PIXELS);
		verticalSplitPanel.setLocked(true);
		verticalSplitPanel.setHeight("110px");
		verticalSplitPanel.setWidth("100%");

		horizontalSplitPanel = new HorizontalSplitPanel();
		horizontalSplitPanel.setSplitPosition(80, Sizeable.UNITS_PERCENTAGE);
		horizontalSplitPanel.setHeight("600px");
		horizontalSplitPanel.setLocked(true);

		loggedOptionGroup = new OptionGroup();
		loggedOptionGroup.addItem("Neodhlašovat");
		loggedOptionGroup.setMultiSelect(true);

		menuItem1.addListener(new ContentSwitcher(this,
				ContentSwitcher.HOME_PANEL));
		menuItem2.addListener(new ContentSwitcher(this,
				ContentSwitcher.AUTOMOBIL_PANEL));
		menuItem3.addListener(new ContentSwitcher(this,
				ContentSwitcher.DOPLNKY_PANEL));
		menuItem4.addListener(new ContentSwitcher(this,
				ContentSwitcher.REGISTRATION_PANEL));
		menuItem6.addListener(new ContentSwitcher(this,
				ContentSwitcher.POJISTENI_PANEL));
		menuItem7.addListener(new ContentSwitcher(this,
				ContentSwitcher.OBJEDNAVKA_PANEL));
		menuItem8.addListener(new ContentSwitcher(this,
				ContentSwitcher.BANKY_PANEL));
		menuItem9.addListener(new ContentSwitcher(this,
				ContentSwitcher.PUJCKY_PANEL));

		regiterButton.addListener(new ContentSwitcher(this,
				ContentSwitcher.REGISTRATION_PANEL));

		menuLayout = new HorizontalLayout();
		menuLayout.addComponent(menuItem1);
		menuLayout.addComponent(menuItem2);
		menuLayout.addComponent(menuItem3);
		menuLayout.addComponent(menuItem7);
		menuLayout.addComponent(menuItem6);
		menuLayout.addComponent(menuItem4);
		menuLayout.addComponent(menuItem8);
		menuLayout.addComponent(menuItem9);

		menuItem8.setVisible(false);
		menuItem9.setVisible(false);

		titleLayout = new VerticalLayout();
		titleLayout.setSpacing(true);
		titleLayout.addComponent(title);
		titleLayout.addComponent(menuLayout);

		verticalSplitPanel.addComponent(titleLayout);
		verticalSplitPanel.addComponent(horizontalSplitPanel);

		loginLayout = new VerticalLayout();

		horizontalSplitPanel.addComponent(switcher);
		initLoginLayout();
		horizontalSplitPanel.addComponent(loginLayout);

		mainLayout = new VerticalLayout();
		mainLayout.addComponent(verticalSplitPanel);
		mainLayout.addComponent(horizontalSplitPanel);

		initButtons(1);
		this.addComponent(mainLayout);
		switcher.setContent(ContentSwitcher.HOME_PANEL);
	}

	public void initButtons(int id_role) {
		menuLayout.removeAllComponents();
		menuLayout.addComponent(menuItem1);
		menuLayout.addComponent(menuItem4);
		menuLayout.addComponent(menuItem2);
		menuLayout.addComponent(menuItem3);
		menuLayout.addComponent(menuItem7);
		menuLayout.addComponent(menuItem6);
		menuLayout.addComponent(menuItem8);
		menuLayout.addComponent(menuItem9);

		MainGuiWindowStateHolder.setState(
				MainGuiWindowStateHolder.NORMAL_STATE, id_role, this);
	}

	public void reInit(int id_role) {
		horizontalSplitPanel.removeComponent(switcher);
		switcher = new ContentSwitcher(this, ContentSwitcher.HOME_PANEL);
		horizontalSplitPanel.addComponent(switcher);
		initButtons(id_role);
	}

	public void initLoginLayout() {
		loginLayout.removeAllComponents();
		loginLayout.setSpacing(true);
		loginLayout.setMargin(true);
		loginLayout.addComponent(loginHeader);
		loginLayout.addComponent(username);
		loginLayout.addComponent(password);
		loginLayout.addComponent(loggedOptionGroup);
		loginLayout.addComponent(confirmLoginButton);
		loginLayout.addComponent(registerHeader);
		loginLayout.addComponent(regiterButton);
	}

	public Zakaznik getZakaznik() {
		return zakaznik;
	}

	public void setZakaznik(Zakaznik zakaznik) {
		this.zakaznik = zakaznik;
	}

	public ContentSwitcher getSwitcher() {
		return switcher;
	}

	public Label getTitle() {
		return title;
	}

	public Label getLoginHeader() {
		return loginHeader;
	}

	public Button getMenuItem1() {
		return menuItem1;
	}

	public Button getMenuItem2() {
		return menuItem2;
	}

	public Button getMenuItem3() {
		return menuItem3;
	}

	public Button getMenuItem4() {
		return menuItem4;
	}

	public Button getMenuItem6() {
		return menuItem6;
	}

	public Button getMenuItem7() {
		return menuItem7;
	}

	public Button getConfirmLoginButton() {
		return confirmLoginButton;
	}

	public HorizontalLayout getMenuLayout() {
		return menuLayout;
	}

	public VerticalLayout getMainLayout() {
		return mainLayout;
	}

	public VerticalLayout getTitleLayout() {
		return titleLayout;
	}

	public VerticalLayout getLoginLayout() {
		return loginLayout;
	}

	public TextField getUsername() {
		return username;
	}

	public PasswordField getPassword() {
		return password;
	}

	public OptionGroup getLoggedOptionGroup() {
		return loggedOptionGroup;
	}

	public VerticalSplitPanel getVerticalSplitPanel() {
		return verticalSplitPanel;
	}

	public HorizontalSplitPanel getHorizontalSplitPanel() {
		return horizontalSplitPanel;
	}

	protected Label getContentLabel() {
		return content;
	}

	public Button getMenuItem8() {
		return menuItem8;
	}

	public Button getMenuItem9() {
		return menuItem9;
	}

}
